<template>
  <div class="search-box">
    <!-- 位置信息 -->
    <div class="location">
      <div class="city" @click="cityClick">{{currentCity.cityName}}</div>
      <div class="position">
        <span class="text" >我的位置</span>
        <img src="@/assets/img/home/icon_location.png" alt="">
      </div>
    </div>

    <!-- 日期 -->
    <div class="data-range" @click="showCalendar = true">
      <div class="start">
        <span>入住</span>
        <span class="time">{{ startDateStr }}</span>
      </div>
      <div class="stay">共{{stayCount}}晚</div>
      <div class="end">
        <span>离店</span>
        <span class="time">{{ endDateStr }}</span>
      </div>
    </div>
    <van-calendar 
      v-model:show="showCalendar"
      type="range"
      color="#ff9854"
      @confirm="onConfirm"
      :show-confirm="false"
    />
    <div class="price-counter">
      <div class="price">价格不限</div>
      <div class="count">人数不限</div>
    </div>
    <div class="keyword">关键字/位置/民宿名</div>
    <!-- 热门建议 -->
    <div class="hot-suggest">
      <template v-for="(item, index) in hotSuggests" :key="index">
        <div class="item" :style="{ color: item.tagText.color, background: item.tagText.background.color }">
          {{item.tagText.text}}
        </div>
      </template>
    </div>

    <!-- 搜索按钮 -->
    <div class="search-btn">
      <div class="btn" @click="searchBtnClick">开始搜索</div>
    </div>
  </div>
</template>

<script setup>
  import { computed, ref } from 'vue'
  import { useRouter } from 'vue-router'
  import useCityStore from '@/store/modules/city'
  import useHomeStore from '@/store/modules/home'
  import useMainStore from '@/store/modules/main'
  import { storeToRefs } from 'pinia'
  import { formatMonthDay, getDiffDays } from '@/utils/format_date'

  const router = useRouter()

  defineProps({
    hotSuggests: {
      type:Array,
      default:() => []
    }
  })

  const cityClick = () => {
    router.push("/city")
  }
  const cityStore = useCityStore()
  const { currentCity } = storeToRefs(cityStore)

  // 日期范围的处理
  const mainStore = useMainStore()
  const { startDate, endDate} = storeToRefs(mainStore)

  const startDateStr = computed(()=> formatMonthDay(startDate.value))
  const endDateStr = computed(()=> formatMonthDay(endDate.value))
  const stayCount = ref(getDiffDays(startDate.value, endDate.value))

  const showCalendar = ref(false)
  const onConfirm = (value)=> {
    const selectStartDate = value[0]
    const selectEndDate = value[1]
    mainStore.startDate = selectStartDate
    mainStore.endDate = selectEndDate
    stayCount.value = getDiffDays(selectStartDate,selectEndDate)
    showCalendar.value = false
  }

  // 热门建议
  const homeStore = useHomeStore()
  const { hotSuggests } = storeToRefs(homeStore)

  //搜索
  const searchBtnClick = () => {
    router.push({
      path: "/search",
      query: {
        startDate: startDate.value,
        endDate: endDate.value,
        currentCity:currentCity.value.cityName
      }
    })
  }
</script>

<style lang="less" scoped>
.location {
      display: flex;
      align-items: center;
      height: 44px;
      padding: 0 20px;
      .city {
        flex: 1;
      }
      .position {
        display: flex;
        align-items: center;
        width: 74px;
        .text {
          font-size: 12px;
        }
        img {
          margin-left: 5px;
          width: 18px;
          height: 18px;
        }
      }
}
.data-range {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  align-items: center;
  padding: 0 20px;
  color: #999;
  .start {
    display: flex;
    flex-direction: column;
    
    .time{
      margin-top: 3px;
      color: #333;
      font-size: 15px;
      font-weight: 500;
    }
  }
  .end {
    display: flex;
    flex-direction: column;
    
    .time{
      margin-top: 3px;
      color: #333;
      font-size: 15px;
      font-weight: 500;
    }
  }
}
.price-counter{
  display: flex;
  margin: 20px 20px;
  .price{
    flex: 8;
    color: #999;
  }
  .count {
    flex: 2;
    color: #999;
  }
}
.keyword {
  margin-left: 20px;
  color: #999;
}
.hot-suggest {
  display: flex;
  flex-wrap: wrap;
  margin-left: 16px;
  margin-top: 20px;
  .item {
    padding: 5px 10px;
    margin: 2px 2px;
    border-radius: 10px;
  }
}
.search-btn {
  display: flex;
  justify-content: center;
  .btn {
    width: 342px;
    height: 38px;
    font-weight: 500;
    font-size: 18px;
    line-height: 38px;
    text-align: center;
    border-radius: 20px;
    margin-top: 20px;
    color: #fff;
    background-color: #fa8c1d;
  }
}
</style>
